package adapletplugin.editors;

import objects.File;
import objects.IObject;
import objects.Table;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import sections.ISection.Section;
import util.ISectionListener;

public class FileEditor extends AbstractSubEditor implements ISectionListener
{

	private Text fIdentifierText;
	private Combo fBaseObjectText;
	private Combo fAliasText;
	private Combo fFilenameText;
	private Combo fFormatText;
	private Combo fSizeText;
	private Combo fStoreText;
	private Combo fLocationText;
	private Combo fTableText;
	private Combo fOIDText;

	private File focusFile;

	public FileEditor(ScrolledComposite composite, FormToolkit toolkit, MapFileEditor parentEditor)
	{
		super(composite, toolkit, parentEditor);
		fMap.registerListener(Section.TABLE, this);
		fMap.registerListener(Section.TYPE, this);
		fMap.registerListener(Section.REF_TYPE, this);
	}

	@Override
	protected void create()
	{
		Composite row = fToolkit.createComposite(fSubComposite);
		row.setLayout(new GridLayout(2, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		fToolkit.createLabel(row, "File Identifier:");
		fIdentifierText = fToolkit.createText(row, "", SWT.BORDER | SWT.READ_ONLY);
		fIdentifierText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
//		fIdentifierText.addModifyListener(createModifyListener_Identifier());

		fToolkit.createLabel(row, "Base Object:");
		fBaseObjectText = new Combo(row, SWT.BORDER);
		fBaseObjectText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fBaseObjectText.addModifyListener(createModifyListener_BaseObject());

		fToolkit.createLabel(row, "Table:");
		fTableText = new Combo(row, SWT.BORDER);
		fTableText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fTableText.addModifyListener(createModifyListener_Table());

		fToolkit.createLabel(row, "OID Column:");
		fOIDText = new Combo(row, SWT.BORDER);
		fOIDText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fOIDText.addModifyListener(createModifyListener_OID());

		fToolkit.createLabel(row, "Alias Column:");
		fAliasText = new Combo(row, SWT.BORDER);
		fAliasText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fAliasText.addModifyListener(createModifyListener_Alias());

		fToolkit.createLabel(row, "Filename Column:");
		fFilenameText = new Combo(row, SWT.BORDER);
		fFilenameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fFilenameText.addModifyListener(createModifyListener_Filename());

		fToolkit.createLabel(row, "Format Column:");
		fFormatText = new Combo(row, SWT.BORDER);
		fFormatText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fFormatText.addModifyListener(createModifyListener_Format());

		fToolkit.createLabel(row, "Size Column:");
		fSizeText = new Combo(row, SWT.BORDER);
		fSizeText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fSizeText.addModifyListener(createModifyListener_Size());

		fToolkit.createLabel(row, "Store Column:");
		fStoreText = new Combo(row, SWT.BORDER);
		fStoreText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fStoreText.addModifyListener(createModifyListener_Store());

		fToolkit.createLabel(row, "Location Column:");
		fLocationText = new Combo(row, SWT.BORDER);
		fLocationText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fLocationText.addModifyListener(createModifyListener_Location());
	}
	
	@Override
	protected void populateColumnComboChoices()
	{
		String tableName = fTableText.getText();
		if(tableName == null || tableName.isEmpty())
		{
			setColumnNames(new String[0]);
			return;
		}
		
		Table table = fMap.getTableSection().getTable(tableName);
		if(table == null)
		{
			setColumnNames(new String[0]);
			return;
		}
		
		String[] columnNames = table.getColumnNames();
		
		setColumnNames(columnNames);
	}

	@Override
	protected void populateTableComboChoices()
	{
		IObject[] tables = fMap.getTableSection().getObjects();
		
		String currentText = fTableText.getText();
		
		fTableText.removeAll();
		
		for(IObject table : tables)
		{
			fTableText.add(table.getObjectName());
		}
		
		fTableText.setText(currentText);
	}

	private ModifyListener createModifyListener_Location()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setLocation(fLocationText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Store()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setStore(fStoreText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Size()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setSize(fSizeText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Format()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setFormat(fFormatText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Filename()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setFilename(fFilenameText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Alias()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setAlias(fAliasText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_OID()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setOID(fOIDText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Table()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setTable(fTableText.getText());
					populateColumnComboChoices();
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_BaseObject()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusFile != null)
				{
					focusFile.setBaseObject(fBaseObjectText.getText());
					fEditor.refresh();
				}
			}
		};
	}

//	private ModifyListener createModifyListener_Identifier()
//	{
//		return new ModifyListener() {
//
//			@Override
//			public void modifyText(ModifyEvent e)
//			{
//				if (focusFile != null)
//				{
//					focusFile.setIdentifier(fIdentifierText.getText());
//					fEditor.refresh();
//				}
//			}
//		};
//	}

	@Override
	protected void updateFields()
	{
		focusFile = (File) fFocusObject;

		if (focusFile == null)
		{
			fIdentifierText.setText("");
			fBaseObjectText.setText("");
			fAliasText.setText("");
			fFilenameText.setText("");
			fFormatText.setText("");
			fSizeText.setText("");
			fStoreText.setText("");
			fLocationText.setText("");
			fTableText.setText("");
			fOIDText.setText("");
		}
		else
		{
			fIdentifierText.setText(focusFile.getIdentifier());
			fBaseObjectText.setText(focusFile.getBaseObject());
			fAliasText.setText(focusFile.getAlias());
			fFilenameText.setText(focusFile.getFilename());
			fFormatText.setText(focusFile.getFormat());
			fSizeText.setText(focusFile.getSize());
			fStoreText.setText(focusFile.getStore());
			fLocationText.setText(focusFile.getLocation());
			fTableText.setText(focusFile.getTable());
			fOIDText.setText(focusFile.getOID());
		}
		
		populateTableComboChoices();
		populateColumnComboChoices();
		populateTypeComboChoices();
	}

	@Override
	public void acceptChange(Section changedSection)
	{
		switch(changedSection)
		{
		case TABLE:
			populateTableComboChoices();
			populateColumnComboChoices();
			break;
		case TYPE:	//$FALL-THROUGH$
		case REF_TYPE:
			populateTypeComboChoices();
			break;
		default:
			break;
		}
		
	}

	private void populateTypeComboChoices()
	{
		IObject[] typeNames = fMap.getTypeSection().getObjects();
		IObject[] refTypeNames = fMap.getReferenceTypeSection().getObjects();
		
		String[] joinedNames = new String[typeNames.length + refTypeNames.length];
		
		int i;
		for(i = 0; i < typeNames.length; i++)
		{
			joinedNames[i] = typeNames[i].getObjectName();
		}
		
		for(int j = 0; j < refTypeNames.length; j++)
		{
			joinedNames[i+j] = refTypeNames[j].getObjectName();
		}
		
		updateComboSelection(fBaseObjectText, joinedNames, focusFile == null ? "" : focusFile.getBaseObject());
	}

	@Override
	protected void setColumnNames(String[] columnNames)
	{
		updateComboSelection(fAliasText, columnNames, focusFile == null ? "" : focusFile.getAlias());
		updateComboSelection(fFilenameText, columnNames, focusFile == null ? "" : focusFile.getFilename());
		updateComboSelection(fFormatText, columnNames, focusFile == null ? "" : focusFile.getFormat());
		updateComboSelection(fSizeText, columnNames, focusFile == null ? "" : focusFile.getSize());
		updateComboSelection(fStoreText, columnNames, focusFile == null ? "" : focusFile.getStore());
		updateComboSelection(fLocationText, columnNames, focusFile == null ? "" : focusFile.getLocation());
		updateComboSelection(fOIDText, columnNames, focusFile == null ? "" : focusFile.getOID());
	}

}
